package Service;
import java.util.Comparator;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import JdbcUitils.FromGpsToBaidu;
import JdbcUitils.GetDistanceByGPSArrs;
import JdbcUitils.InsertCyclingFromImeiDao;
import JdbcUitils.StringUtil;
public class InsertIntoIMEIinfoFromSocketSer {

//	@Resource(name="imeiInfoDao")
//	private IImeiInfoDao imeiInfoDao;
	public static void main(String[] args) {
		//String info="862991528907295,89860617080015741141,6936.8C15,170721120809,E,110.3352866,N,25.2428350,0,5.000,60.000,4.2,1,00";
		//new InsertIntoIMEIinfoFromSocketSer().InsertIntoIMEIinfo(info);
//		ArrayList<String> listgps = new ArrayList<String>();
//		listgps.add(0,"ooo");
//		listgps.add(1,"qqq");
//		listgps.add(2,"www");
//		listgps.add(3,"eee");
//		listgps.add(4,"rrr");
//		System.out.println("==="+StringUtils.join(listgps.toArray(),"|"));
	}
	
	//根据硬件故障码获取故障码描述
	public String getDescByFalutcode(String code){
		InsertCyclingFromImeiDao fromImeiDao = new InsertCyclingFromImeiDao();
		String sql = "select fault_code_info from fault where fault_code = ?";
		String[] psarr = {code};
		
		return fromImeiDao.getStrByColumnDao(sql,psarr);
	}
	//根据IMEI硬件编号获取公司id
	public String getBusinessIdByIMEI(String imei){
		InsertCyclingFromImeiDao fromImeiDao = new InsertCyclingFromImeiDao();
		String sql = "select business_id from user_vehicle where imie = ?";
		String[] psarr = {imei};
		return fromImeiDao.getStrByColumnDao(sql,psarr);
	}
	//向硬件故障表中插入数据
	public void insertIntoHardwareFault(String[] psarr){
		InsertCyclingFromImeiDao fromImeiDao = new InsertCyclingFromImeiDao();
		String sql = "insert into hardware_fault values(replace(uuid(),'-',''),?,?,?,Now(),Now(),1,?,?,?,?)";
		
		fromImeiDao.insertTableByArray(sql,psarr);
	}
	//根据故障发生的GPS位置查询附近的检修点
	public Map<String,ArrayList<String>> getNearlyChevkpointBybusinessId(String businessId){
		InsertCyclingFromImeiDao fromImeiDao = new InsertCyclingFromImeiDao();
		
		String sql = "select gps,address from business_checkpoint where business_id='"+businessId+"'";
		
		return fromImeiDao.getssmapfromcheckpoint(sql);
	}
	//根据检修点的GPSlist以及addresslist获取最近的距离
	public TreeMap<Integer, String> getMinDistance(String startGps,int sum,ArrayList<String> listgps,ArrayList<String> listaddress){
		TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(new Comparator<Integer>() {
            public int compare(Integer a,Integer b){
                  return a-b;
            }  
		});
		if(listgps.size()==0){
			TreeMap<Integer, String> treeMapNull = new TreeMap<Integer, String>();
        	treeMapNull.put(0, "暂无检修点");
        	return treeMapNull;
		}else{
			for(int i = 0;i<(listgps.size()/50+1);i++){
				List<String> listgpsMin = new ArrayList<String>();
				System.out.println(listgps.size()+"=======");
				if(50*(i+1)>listgps.size()){
					listgpsMin = listgps.subList(i*50,listgps.size()-1);
				}else{
					listgpsMin = listgps.subList(i*50,i*50+50);
				}
				String endGpsS = StringUtils.join(listgpsMin.toArray(),"|");
				String result = new GetDistanceByGPSArrs().getDistanceByGPSArrsSer(startGps, endGpsS);
				JSONObject jsStr = JSONObject.parseObject(result);
				JSONArray jsarry = JSONArray.parseArray(jsStr.get("result").toString());
				for(int m=0;m<jsarry.size();m++){
					String str = ((JSONObject)JSONObject.parseObject(jsarry.get(m).toString()).getJSONObject("distance")).getString("text");
					//System.out.println(JSONObject.parseObject(jsarry.get(0).toString()).getJSONObject("distance")+"========");
					//System.out.println(str+"-000000-----");
					//key为最后获得的10的公里里程
					String temp = str.replace("公里", "").replace(".", "");
					//String temp = JSONObject.parseObject(jsarry.get(0).toString()).getJSONObject("distance").getJSONObject("text").toString().replace("公里", "");
					treeMap.put(Integer.parseInt(temp),listaddress.get(i*50+m));
				}
			}
	        System.out.println(treeMap.toString());
	        System.out.println(treeMap.firstEntry().getKey()+"====="+treeMap.firstEntry().getValue());
	        if(sum==1){
	        	//系统自动分配的
	        	TreeMap<Integer, String> treeMapOne = new TreeMap<Integer, String>();
	        	treeMapOne.put(treeMap.firstEntry().getKey(), treeMap.firstEntry().getValue());
	        	return treeMapOne;
	        }else{
	        	//改派
	        	return treeMap;
	        }
		}
		
		
	}
	//将硬件发送的数据存入数据库中
	public void InsertIntoIMEIinfo(String socketInfo){
		InsertCyclingFromImeiDao fromImeiDao = new InsertCyclingFromImeiDao();
		String[] infoSplit = socketInfo.split(",");
		String sql1 = "INSERT INTO imei_info VALUES(replace(uuid(),'-',''),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		fromImeiDao.InsertIntoIMEIinfoDao(sql1, infoSplit);
		
	}

	
	public void UpdateOrInsertIMEIinfonew(String infoStr,String statisticBehavior,String offline) {
		// TODO Auto-generated method stub
		InsertCyclingFromImeiDao fromImeiDao = new InsertCyclingFromImeiDao();
		String sql1 = "delete from imei_info_new where imei='"+infoStr.split(",")[0]+"'";
		fromImeiDao.deleteTableRowDao(sql1);
		
		if(!"3".equals(statisticBehavior)){
			//在线
			statisticBehavior = "1";
		}	
		//跟新数据
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		calendar.setTime(new Date());
		calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - Integer.parseInt(offline));  
		String time1 = sf.format(calendar.getTime());
		String sql3 = "update imei_info_new set statistic_behavior = 2 where  cast(time as datetime) < cast('"+time1+"' as datetime)";
		fromImeiDao.deleteTableRowDao(sql3);//这里的delete是执行的更新功能
		
		String[] infoSplit = (infoStr+","+statisticBehavior).split(",");
		String sql2 = "INSERT INTO imei_info_new VALUES(replace(uuid(),'-',''),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		fromImeiDao.InsertIntoIMEIinfoDao(sql2, infoSplit);
		
	}

	public String getspeedValueByspeedname(String string) {
		// TODO Auto-generated method stub
		InsertCyclingFromImeiDao fromImeiDao = new InsertCyclingFromImeiDao();
		String sql1 = "select value from global_variable where name=?";
		String[] psarr = {string};
		
		return fromImeiDao.getStrByColumnDao(sql1, psarr);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
